package com.atguigu.gmall.pms.service.impl;

import com.atguigu.gmall.pms.entity.AttrEntity;
import com.atguigu.gmall.pms.entity.SkuAttrValueEntity;
import com.atguigu.gmall.pms.entity.SpuAttrValueEntity;
import com.atguigu.gmall.pms.service.AttrService;
import com.atguigu.gmall.pms.service.SkuAttrValueService;
import com.atguigu.gmall.pms.service.SpuAttrValueService;
import com.atguigu.gmall.pms.vo.AttrValueVo;
import com.atguigu.gmall.pms.vo.GroupVo;
import com.atguigu.gmall.pms.vo.ItemGroupVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.atguigu.gmall.common.bean.PageResultVo;
import com.atguigu.gmall.common.bean.PageParamVo;

import com.atguigu.gmall.pms.mapper.AttrGroupMapper;
import com.atguigu.gmall.pms.entity.AttrGroupEntity;
import com.atguigu.gmall.pms.service.AttrGroupService;
import org.springframework.util.CollectionUtils;


@Service("attrGroupService")
public class AttrGroupServiceImpl extends ServiceImpl<AttrGroupMapper, AttrGroupEntity> implements AttrGroupService {

    @Autowired
    AttrService attrService;
    @Autowired
    SpuAttrValueService spuAttrValueService;
    @Autowired
    SkuAttrValueService skuAttrValueService;
    @Override
    public PageResultVo queryPage(PageParamVo paramVo) {
        IPage<AttrGroupEntity> page = this.page(
                paramVo.getPage(),
                new QueryWrapper<AttrGroupEntity>()
        );

        return new PageResultVo(page);
    }

    /*
    根据分类信息 查询该分类下的组和组下的信息
     */
    @Override
    public List<GroupVo> queryGroupsWithAttrsByCid(Long cid) {
        if (cid==0){
            return null;
        }
        List<AttrGroupEntity> attrGroupEntities = this.baseMapper.selectList(new QueryWrapper<AttrGroupEntity>().eq("category_id", cid));
        List<GroupVo> collect = attrGroupEntities.stream().map(item -> {
            GroupVo groupVo = new GroupVo();
            //查询组下的参数信息
            List<AttrEntity> attrEntities = attrService.getByCategoryIdAndGroupId(item.getCategoryId(), item.getId());
            BeanUtils.copyProperties(item, groupVo);
            groupVo.setAttrEntities(attrEntities);
            return groupVo;
        }).collect(Collectors.toList());
        return collect;
    }

    @Override
    public List<AttrGroupEntity> queryAttrGroupCid(Long cid) {
        List<AttrGroupEntity> attrGroupEntities = this.baseMapper.selectList(new QueryWrapper<AttrGroupEntity>().eq("category_id", cid));
        return attrGroupEntities;
    }

    @Override
    public List<ItemGroupVo> queryGroupsWitAttrsAndValuesByCidAndSkuIdAndSpuId(Long cid, Long skuId, Long spuId) {
        //查询分类下的规格参数
        List<AttrGroupEntity> attrGroupEntities = this.baseMapper.selectList(new QueryWrapper<AttrGroupEntity>().eq("category_id", cid));
        if (CollectionUtils.isEmpty(attrGroupEntities)){
            return null;
        }
        List<ItemGroupVo> itemGroupVos = new ArrayList<>();
        //查询每个组下对应的规格参数
        List<ItemGroupVo> list = attrGroupEntities.stream().map(attrGroupEntity -> {
            ItemGroupVo itemGroupVo = new ItemGroupVo();
            itemGroupVo.setId(attrGroupEntity.getId());
            itemGroupVo.setName(attrGroupEntity.getName());
            List<AttrEntity> attrEntities = attrService.list(new QueryWrapper<AttrEntity>().eq("group_id", attrGroupEntity.getId()));
            if(!CollectionUtils.isEmpty(attrEntities)){
                List<Long> collect = attrEntities.stream().map(AttrEntity::getId).collect(Collectors.toList());
                //根据规格参数id查询对应的sku规格参数
                List<SkuAttrValueEntity> attr_id = skuAttrValueService.list(new QueryWrapper<SkuAttrValueEntity>().eq("sku_id", skuId).in("attr_id", collect));
                if (!CollectionUtils.isEmpty(attr_id)) {
                    List<AttrValueVo> attrValueVos = new ArrayList<>();
                    attrValueVos.addAll(attr_id.stream().map(item -> {
                        AttrValueVo attrValueVo = new AttrValueVo();
                        BeanUtils.copyProperties(item, attrValueVo);
                        return attrValueVo;
                    }).collect(Collectors.toList()));
                    itemGroupVo.setAttrs(attrValueVos);
                }

                //根据规格参数id查询对应的spu规格参数
                List<SpuAttrValueEntity> spuAttrValueEntities = spuAttrValueService.list(new QueryWrapper<SpuAttrValueEntity>().eq("spu_id", spuId).in("attr_id", collect));
                if (!CollectionUtils.isEmpty(spuAttrValueEntities)) {
                    List<AttrValueVo> attrValueVos = new ArrayList<>();
                    attrValueVos.addAll(spuAttrValueEntities.stream().map(item -> {
                        AttrValueVo attrValueVo = new AttrValueVo();
                        BeanUtils.copyProperties(item, attrValueVo);
                        return attrValueVo;
                    }).collect(Collectors.toList()));
                    itemGroupVo.setAttrs(attrValueVos);
                }
            }
            return itemGroupVo;
        }).collect(Collectors.toList());

        return list;
    }

}